package common

import (
	"Management/model"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)
//连接mysql
func InitDB()*gorm.DB {
	dsn := "root:123456@(localhost:3306)/management?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err !=nil{
		panic(err)
	}
	//自动生产sql语句
	db.AutoMigrate(&model.Loginer{},&model.Department{},&model.User{})
	sqlDB, err := db.DB()
	if err != nil {
		fmt.Printf("mysql error: %s", err)
	}

	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(100)
	sqlDB.SetConnMaxLifetime(5*time.Second)//5秒内关闭mysql连接

	return db
}